package hotel.server;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Collection;

import hotel.rmi.HotelService;
import hotel.rmi.to.Guest;
import hotel.rmi.to.Room;


@SuppressWarnings("serial")
public class HotelServiceImpl extends java.rmi.server.UnicastRemoteObject
		implements HotelService {

	ArrayList<Guest> guests = new ArrayList<Guest>();
	
	ArrayList<Room> rooms = new ArrayList<Room>();
	
	HotelServiceImpl() throws java.rmi.RemoteException {
		super();
		rooms.add(new Room(1, 10, 150));
		rooms.add(new Room(2, 20, 120));
		rooms.add(new Room(3, 20, 100));		
	}

	Room getRoomByType(int type) {
		for (Room room: rooms)
			if (room.getId() == type)
				return room;
		return null;
	}
	
	synchronized public void book(int type, String name) throws RemoteException {
		Room room = getRoomByType(type);
		if (room == null)
			throw new RoomException("Not such room type");
		else if (room.getFreeCount() == 0)
			throw new RoomException("Not any places left");
		
		room.book(1);
		guests.add(new Guest(name));
	}

	synchronized public Collection<Room> getAvailableRooms() throws RemoteException {
		Collection<Room> result = new ArrayList<Room>();
		for (Room room: rooms)
			if (room.getFreeCount() > 0)
				result.add(room);
		return result;
	}

	synchronized public Collection<Guest> getGuests() throws RemoteException {
		return guests;
	}


}
